package com.redpill.linpro.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.redpill.linpro.integration.Account;
import com.redpill.linpro.integration.Amount;
import com.redpill.linpro.service.AccountService;

public class AccountTransactionDecorator implements AccountService {

	private final AccountService accountService;
	
	private final Logger logger = LoggerFactory.getLogger(AccountTransactionDecorator.class);
	
	private int commitCounter = 0;
	
	private int rollbackCounter = 0;
	
	public AccountTransactionDecorator(AccountService accountService) {
		this.accountService = accountService;
	}

	@Override
	public boolean transferAmount(Amount amount, Account debetAccount,
			Account creditAccount) {
		final boolean transferred;
		
		logger.debug("Begin transaction");
		
		try {
			transferred = accountService.transferAmount(amount, debetAccount, creditAccount);
			commitCounter++;
			logger.debug("Commit transaction");			
		} catch (RuntimeException e) {
			rollbackCounter++;
			logger.debug("Rollback transaction");
			throw e;
		}
		return transferred;
	}
	
	public int getCommitCounter() {
		return commitCounter;
	}
	
	public int getRollbackCounter() {
		return rollbackCounter;
	}
}
